import api from '../../api';
import { message } from 'antd';
import { forEach } from 'lodash';

export default {
  namespace: 'personnelManagement', //交易对手黑白名单管理
  state: {
    mainIncomObj: '12345567788', //主营业务表格
    saveVisible: false,
    portfolioList: [],

    shortName: [],
    tableData: [],
    TablepageInfo: [],

    whiteTableData: [],
    whiteTablepageInfo: {},
    blackTableData: [],
    blackTablepageInfo: {},
    statusEnum: [],
  },
  effects: {
    // 删除名单
    *deleteBWlist({ payload, callback }, { call, put, select }) {
      const res = yield call(api.personnelManagement.deleteBWlist, payload);
      if (res) {
        if (res.code == 0) {
          message.success(res.msg || '删除成功!');
          callback('success');
        } else {
          message.error(res.msg || '删除失败！');
        }
      } else {
        message.error('删除失败！');
      }
    },
    // 查询列表
    *getBWList({ payload, callback }, { call, put, select }) {
      const res = yield call(api.personnelManagement.getBWList, payload);
      if (res) {
        if (res.code == 0) {
          if (payload.type == 'black') {
            yield put({
              type: 'save',
              payload: {
                blackTableData: res.data.dataSource || [],
                blackTablepageInfo: res.data.pageInfo || {},
              },
            });
          }

          if (payload.type == 'white') {
            yield put({
              type: 'save',
              payload: {
                whiteTableData: res.data.dataSource || [],
                whiteTablepageInfo: res.data.pageInfo || {},
              },
            });
          }

          callback(res.data);
          // yield put({
          //     type: 'save',
          //     payload: {
          //         tableData:res.data.dataSource || [],
          //     }
          // });
        } else {
          yield put({
            type: 'save',
            payload: {
              tableData: [],
            },
          });
          message.error(res.msg || '查询列表失败!');
        }
      } else {
        yield put({
          type: 'save',
          payload: {
            tableData: [],
          },
        });
        message.error('查询列表失败!');
      }
    },
    // 新增或者编辑交易对手详情
    *insertBWlist({ payload, callback }, { call, put, select }) {
      const res = yield call(api.personnelManagement.insertBWlist, payload);
      if (res) {
        if (res.code == 0) {
          message.success(res.msg || '新增成功！');
          callback('success');
        } else {
          message.error(res.msg || '新增失败！');
        }
      } else {
        message.error('新增失败！');
      }
    },

    //查询短名
    *getShortName({ payload, callback }, { call, put, select }) {
      const res = yield call(api.personnelManagement.getShortName, payload);
      if (res && res.code == 0) {
        yield put({
          type: 'save',
          payload: {
            shortName: res.data,
          },
        });
      } else {
        message.error('查询短名失败!');
      }
    },
  },
  reducers: {
    save(state, { payload }) {
      return { ...state, ...payload };
    },
  },
};
